<template>
  <div class="dialog-overlay" v-if="show" @click="closeDialog">
    <div class="dialog-content" @click.stop>
      <div class="dialog-header">
        <h2>{{ title }}</h2>
        <button class="close-btn" @click="closeDialog">
          <font-awesome-icon icon="times" />
        </button>
      </div>

      <component 
        :is="currentComponent" 
        :formData="formData"
        @update:formData="updateFormData"
        ref="appointmentForm"
      />

      <div class="dialog-footer">
        <button class="cancel-btn" @click="closeDialog">取消</button>
        <button class="submit-btn" @click="submitAppointment">确认预约</button>
      </div>
    </div>
  </div>
</template>

<script>
import MedicineAppointment from './MedicineAppointment.vue';
import MealAppointment from './MealAppointment.vue';
import CleaningAppointment from './CleaningAppointment.vue';
import ShoppingAppointment from './ShoppingAppointment.vue';

export default {
  name: 'AppointmentDialog',
  components: {
    MedicineAppointment,
    MealAppointment,
    CleaningAppointment,
    ShoppingAppointment
  },
  props: {
    show: {
      type: Boolean,
      required: true
    },
    type: {
      type: String,
      required: true
    },
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      formData: {
        date: '',
        time: '',
        duration: '2',
        requirements: '',
        mealId: null,
        note: '',
        items: [{ name: '', quantity: 1 }],
        medicines: []
      }
    };
  },
  computed: {
    currentComponent() {
      const componentMap = {
        medicine: 'MedicineAppointment',
        meal: 'MealAppointment',
        cleaning: 'CleaningAppointment',
        shopping: 'ShoppingAppointment'
      };
      return componentMap[this.type];
    }
  },
  methods: {
    closeDialog() {
      this.$emit('close');
      this.resetForm();
    },
    updateFormData(newData) {
      this.formData = newData;
    },
    submitAppointment() {
      if (!this.formData.date || !this.formData.time) {
        alert('请选择预约日期和时间');
        return;
      }

      const validationError = this.$refs.appointmentForm?.validate();
      if (validationError) {
        alert(validationError);
        return;
      }

      this.$emit('submit', {
        type: this.type,
        ...this.formData
      });
      
      this.closeDialog();
    },
    resetForm() {
      this.formData = {
        date: '',
        time: '',
        duration: '2',
        requirements: '',
        mealId: null,
        note: '',
        items: [{ name: '', quantity: 1 }],
        medicines: []
      };
    }
  }
};
</script>

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-content {
  background: white;
  border-radius: 16px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.dialog-header {
  padding: 20px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-header h2 {
  margin: 0;
  color: #2c3e50;
  font-size: 24px;
}

.close-btn {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  font-size: 20px;
  padding: 5px;
}

.close-btn:hover {
  color: #333;
}

.dialog-footer {
  padding: 20px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn,
.submit-btn {
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.cancel-btn {
  background: white;
  border: 1px solid #dcdfe6;
  color: #606266;
}

.submit-btn {
  background: #3498db;
  border: none;
  color: white;
}

.cancel-btn:hover {
  border-color: #c0c4cc;
  color: #409eff;
}

.submit-btn:hover {
  background: #409eff;
}

/* 通用表单样式 */
:deep(.dialog-body) {
  padding: 20px;
}

:deep(.form-group) {
  margin-bottom: 20px;
}

:deep(.form-group label) {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-weight: 500;
}

:deep(.form-input) {
  width: 100%;
  padding: 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
}

:deep(.form-input:focus) {
  border-color: #3498db;
  outline: none;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}
</style> 